package api

import (
	"github.com/gin-gonic/gin"
	"lib/http"
	"lib/structs/vap"
	"vap/service"
)

var ShHostApi = new(shHostApi)

type shHostApi struct {
}

func (a shHostApi) Add(c *gin.Context) {
	var in vap.AddShHost
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	u := http.GetCurrentUser(c)
	err := service.ShHostServ.Add(in, u)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a shHostApi) Delete(c *gin.Context) {
	in := struct {
		Id int64 `uri:"id,string" binding:"required"`
	}{}
	if err := c.ShouldBindUri(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	u := http.GetCurrentUser(c)
	err := service.ShHostServ.Delete(in.Id, u)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a shHostApi) Page(c *gin.Context) {
	var in vap.SearchShHost
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.ShHostServ.Page(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}
